<?php

namespace app\adminmall\controller;
use app\adminmall\model\UnionStoreModel;
use app\common\controller\AdminBaseController;
use think\Db;
/**
 * 联盟商家
 */
class UnionStoreController extends AdminBaseController
{
 	 protected $model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new UnionStoreModel();
    }
    /**
     * 联盟商家列表
     * @author [longx] 
     */
    public function index( $page = 1){
        $list = $this->model->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }
    /**
     * 查询
     * @author [longx] 
     */
    public function search(){
     $map=[];
           
       if(request()->isPost()){
		 	$param = input('post.');
          
			//联盟名称
			if($param['name']){
				$map['name'] = ['like', '%'.$param['name'].'%'];
			}
			//审核状态
			if($param['status']){
				$map['status'] = ['like', '%'.$param['status'].'%'];
			}
			//负责人名称
			if($param['shopowner']){
				$map['shopowner'] = ['like', '%'.$param['shopowner'].'%'];
			}
		    //创建时间
			if($param['createtime']&&$param['end_time']){
				$map['createtime'] = ['between time', [$param['createtime'],$param['end_time']]];
			}
			
			
  	    $list=Db::name("mall_unionstore")
       ->where($map)
       ->paginate(10);

		
        return $this->fetch('index', ['list' => $list]);
      }
    }


     /**
      * 冻结
      */
       public function frzee($id,$state){
       	 $id = input('param.id');
       	 $param['state'] = input('param.state');

       	 if($param['state'] == '1'){
       	 	$param['state']='2';
       	 }else{
       	 	$param['state']='1';
       	 }
       	 Db::table('long_mall_unionstore')->where('id',$id)->update(['state'=> $param['state']]);
       	 $list=Db::name("mall_unionstore")->paginate(10);
  return $this->fetch('index',['list'=>$list]);
       }
      
      
      /**
       * 查看联盟成员
       */
      public function show($id){
      	$id=input('param.id');
      	$data = Db::name('mall_unionstore')->field('rules')->where('id',$id)->find(); 
        $rules      = explode(',', $data['rules']);

      foreach( $rules as $k=>$v){
        $list[]  = Db::name('mall_agent')->find($v);
      }

       return $this->fetch('',['list'=> $list]);
    }
      
    /**
     * [edit 审核]
     * @return [type] [description]
     * @author [longx]
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');  
			$param['audittime'] = now_time();       
			//审核通过状态为2
			$param['status'] = '2';
			if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('审核成功', url('union_store/index'));
            } else {
                $this->error('审核失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }



    /**
     * [del删除]
     * @author [longx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
     /**
     * [eexamines 审核]
     * @return [type] [description]
     * @author [longx]
     */
    public function examines($id)
    {
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }
    /**
    
    /**
     * [ 审核通过]
     * @return [type] [description]
     * @author [longx]
     */
    public function pass($id){
    	
    	  if(request()){
            $param = input('post.');
            $param['audittime'] = now_time();       
			//审核通过状态为2
			
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('Union_store/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }
}
